package client.ui;

import java.util.List;

import phasebook.client.msgs.UserProfileInfo;

import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HeaderControl;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.IntegerItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

public class LoginAndRegister implements ResponseHandler {
	private Controller controller;
	private Window registerWindow = new RegisterWindow();
	private Window loginWindow = new LoginWindow();

	public LoginAndRegister(Controller controller) {
		this.controller = controller;
		controller.addResponseHandler(this);
		loginWindow.show();
	}
	
	private class LoginWindow extends Window implements ChangedHandler{
		private TextItem usernameItem = new TextItem();
		private PasswordItem passwordItem = new PasswordItem();
		private Button ok = new Button("Login");
		
		private LoginWindow(){
			HeaderControl plus = new HeaderControl(HeaderControl.PLUS,
					new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							hide();
							registerWindow.show();
						}
					});
			setHeaderControls(HeaderControls.HEADER_LABEL, plus);
			setWidth(230);
			setHeight(105);
			setTitle("Please Login");
			setShowMinimizeButton(false);
			setIsModal(true);
			setShowModalMask(true);
			centerInPage();
	
			final DynamicForm form = new DynamicForm();
			form.setWidth100();
	
			usernameItem.setValue("");
			usernameItem.setTitle("Username");
			usernameItem.setRequired(true);
			usernameItem.addChangedHandler(this);
	
			passwordItem.setValue("");
			passwordItem.setTitle("Password");
			passwordItem.setRequired(true);
			passwordItem.addChangedHandler(this);
	
			form.setFields(new FormItem[] { usernameItem, passwordItem });
	
			ok.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					controller.validateLogin(
						usernameItem.getValueAsString(),
						passwordItem.getValueAsString()
					);
					ok.disable();
				}
			});
			ok.disable();
	
			addItem(form);
			addItem(ok);
			setAlign(Alignment.RIGHT);
		}
		@Override
		public void onChanged(ChangedEvent event) {
			if (usernameItem.getValueAsString().length()==0||
					passwordItem.getValueAsString().length()==0)
					ok.disable();
				else
					ok.enable();
		}
	}

	private class RegisterWindow extends Window implements ChangedHandler {
		private TextItem nameItem = new TextItem();
		private TextItem usernameItem = new TextItem();
		private IntegerItem bankAccountIdItem = new IntegerItem();		
		private TextItem emailItem = new TextItem();
		private PasswordItem passwordItem = new PasswordItem();
		private PasswordItem password2Item = new PasswordItem();
		private Button ok = new Button("Register");

		private RegisterWindow(){
			HeaderControl close = new HeaderControl(HeaderControl.CLOSE,
					new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							hide();
							loginWindow.show();
						}
					});
			setHeaderControls(HeaderControls.HEADER_LABEL, close);
			setWidth(280);
			setHeight(220);
			setTitle("Register a new user");
			setShowMinimizeButton(false);
			setIsModal(true);
			setShowModalMask(true);
			centerInPage();
	
			final DynamicForm form = new DynamicForm();
			form.setWidth100();
	
			nameItem.setValue("");
			nameItem.setTitle("Name");
			nameItem.setRequired(true);
			nameItem.addChangedHandler(this);
	
			usernameItem.setValue("");
			usernameItem.setTitle("Username");
			usernameItem.setRequired(true);
			usernameItem.addChangedHandler(this);
	
			
			bankAccountIdItem.setValue("");
			bankAccountIdItem.setTitle("Bank Account Id");
			bankAccountIdItem.setRequired(true);
			bankAccountIdItem.addChangedHandler(this);
			
			emailItem.setValue("");
			emailItem.setTitle("Email");
			emailItem.setRequired(true);
			emailItem.addChangedHandler(this);
	
			passwordItem.setValue("");
			passwordItem.setTitle("Password");
			passwordItem.setRequired(true);
			passwordItem.addChangedHandler(this);
			
			password2Item.setValue("");
			password2Item.setTitle("Password again");
			password2Item.setRequired(true);
			password2Item.addChangedHandler(this);
	
			form.setFields(new FormItem[] { nameItem, usernameItem, bankAccountIdItem, emailItem,
					passwordItem, password2Item });
			
			ok.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					UserProfileInfo req = new UserProfileInfo();
					req.setName(nameItem.getValueAsString());
					req.setBankAccountId(bankAccountIdItem.getValueAsInteger());
					req.setEmail(emailItem.getValueAsString());
					req.setUsername(usernameItem.getValueAsString());
					req.setPassword(passwordItem.getValueAsString());
					controller.registerUser(req);
					ok.disable();
				}
			});
			ok.disable();
	
			addItem(form);
			addItem(ok);
		}

		@Override
		public void onChanged(ChangedEvent event) {
			if (nameItem.getValueAsString().length()==0||
				usernameItem.getValueAsString().length()==0||
				emailItem.getValueAsString().length()==0||
				passwordItem.getValueAsString().length()==0||
				!passwordItem.getValueAsString().equals(password2Item.getValueAsString()))
				ok.disable();
			else
				ok.enable();
		}
	}

	@Override
	public void onResponse(CombinedResponse response) {
		UserProfileInfo userProfile = new UserProfileInfo();
		
		Long userId = response.getUserId();
		
		if (userId!=null){
			List<UserProfileInfo> profiles = response.getProfiles();
			if (profiles != null)
				for (UserProfileInfo p : profiles)
					if (p.getUserId() == userId)
						userProfile=p;
			
			new RootPanel(controller, userProfile).draw();
			controller.removeResponseHandler(this);
			controller.onSuccess(response);
			loginWindow.hide();
			registerWindow.hide();
			controller.updateLoop();
		}
	}
}
